import { defineStore } from 'pinia'
import { getUserInfo, getMenuList, getPermissionList } from '@/api'
import { getDietDict, getSportDict } from '@/api/healthManaWorkbench'
import router, { resetRouter } from '@/router'
import store from '../index'

const getMeunList = (data) => [
  data.map((v) => {
    return {}
  })
]

export const userApp = defineStore('app', {
  state: () => ({
    spinning: false,
    keepAliveRoutes: new Set(),
    userInfo: '',
    menuList: [],
    permissionList: [],
    dietDictList: [],
    sportDictList: [],
    tabs: [
      {
        name: '首页',
        path: '/home',
        closable: false
      }
    ],
    tabsActiveKey: '/home'
  }),
  getters: {},
  actions: {
    Loading(payload: boolean) {
      this.spinning = payload
    },
    keepAliveRoutesPush(payload) {
      this.keepAliveRoutes.add(payload)
    },
    keepAliveRoutesDel(payload) {
      this.keepAliveRoutes.delete(payload)
    },
    keepAliveRoutesClear() {
      this.keepAliveRoutes.clear()
    },
    setUserInfo(payload: g.object) {
      this.userInfo = payload
    },
    async GetUserInfo() {
      const res = await getUserInfo(store.userStore.orgId)
      this.setUserInfo(res.data)
      // this.setUserInfo({
      //   name: 'admin'
      // })
    },
    async GetMenuList() {
      const res = await getMenuList(store.userStore.platformId)
      this.menuList = resetRouter(res.data)
      // const data = [
      //   {
      //     path: '/doctorManagement',
      //     name: 'doctorManagement',
      //     meta: { title: '医生管理', icon: 'FileSearchOutlined' },
      //     component: () => import('@/views/doctorManagement/index.vue')
      //   },
      //   {
      //     path: '/commodityManagement',
      //     name: 'commodityManagement',
      //     meta: { title: '商品管理', icon: 'FileSearchOutlined' },
      //     component: () => import('@/views/commodityManagement/index.vue')
      //   },
      //   {
      //     path: '/patientManagement',
      //     name: 'patientManagement',
      //     meta: { title: '患者管理', icon: 'SolutionOutlined' },
      //     component: () => import('@/views/patientManagement/index.vue')
      //   },
      //   {
      //     path: '/healthManagement',
      //     name: 'healthManagement',
      //     meta: { title: '健管师管理', icon: 'SolutionOutlined' },
      //     component: () => import('@/views/healthManagement/index.vue')
      //   },
      //   {
      //     path: '/templateManage',
      //     name: '模板管理',
      //     meta: { title: '模板管理', icon: 'SettingOutlined' },
      //     component: () => import('@/views/templateManage/index.vue')
      //   }
      // ]
      // this.menuList = resetRouter(data)
    },
    async GetPermissionList() {
      const res = await getPermissionList()
      this.permissionList = res.data
    },
    async getDietDict() {
      const res = await getDietDict()
      res.data.map((item) => {
        item.label = item.name
        item.value = item.id
        item.list.map((child) => {
          child.label = child.dietName
          child.value = child.dietName
        })
        item.options = item.list
      })
      this.dietDictList = res.data
    },
    async getSportDict() {
      const res = await getSportDict()
      res.data.map((item) => {
        item.value = item.sportName
        item.label = item.sportName
      })
      this.sportDictList = res.data
    },
    tabsDel(payload) {
      const i = this.tabs.findIndex((v) => v.path === payload.path)

      if (i > -1) {
        this.tabs.splice(i, 1)
      }

      if (this.tabsActiveKey === payload.path) {
        this.tabsActiveKey = this.tabs[this.tabs.length - 1].path

        router.push({
          path: this.tabsActiveKey
        })
      }
    },
    tabsAdd(payload) {
      if (this.tabs.some((v) => v.path === payload.path)) {
        return
      } else {
        if (store.userStore.hasTab) {
          this.tabs.push(payload)
        } else {
          this.tabs = [
            {
              name: '首页',
              path: '/home',
              closable: false
            },
            payload
          ]
        }
      }
      this.tabsActiveKey = payload.path
    },
    setUrlQuery(payload) {
      const item = this.tabs.find((v) => v.path === this.tabsActiveKey)

      item.query = payload
    }
  }
})
